package evq.codec.ui;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import evq.codec.core.EvqApplication;
import evq.codec.core.entities.EncodeMethod;
import evq.codec.core.entities.Profiler;

/**
 * @author Gutemberg Rodrigues
 * @author Edson Jordão
 */
public class InputQVFrame extends javax.swing.JFrame {

    private JFileChooser jFileChooser;
    private EvqApplication app;
    
    public InputQVFrame() {
        initComponents();
        app = EvqApplication.getInstance();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    }

    @SuppressWarnings("unchecked")

    private void initComponents() {

        painel = new javax.swing.JPanel();
        labelImageOriginal = new javax.swing.JLabel();
        txtImageOriginal = new javax.swing.JTextField();
        txtImageDestination = new javax.swing.JTextField();
        labelImageDestination = new javax.swing.JLabel();
        labelMethod = new javax.swing.JLabel();
        comboMethod = new javax.swing.JComboBox();
        bntSearch1 = new javax.swing.JButton();
        bntSearch2 = new javax.swing.JButton();
        bntQuantifyImage = new javax.swing.JButton();
        bntClean = new javax.swing.JButton();
        bntSearch3 = new javax.swing.JButton();
        txtDictionary = new javax.swing.JTextField();
        labelDictionary = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        painel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        labelImageOriginal.setText("Imagem Original:");

        txtImageDestination.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtImageDestinationActionPerformed(evt);
            }
        });

        labelImageDestination.setText("Imagem Destino:");

        labelMethod.setText("Método:");

        comboMethod.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "FS", "PDS" }));

        bntSearch1.setText("...");
        bntSearch1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntSearch1ActionPerformed(evt);
            }
        });

        bntSearch2.setText("...");
        bntSearch2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntSearch2ActionPerformed(evt);
            }
        });

        bntQuantifyImage.setText("Quantificar Imagem");
        bntQuantifyImage.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntQuantifyImageActionPerformed(evt);
            }
        });

        bntClean.setText("Limpar Campos");
        bntClean.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCleanActionPerformed(evt);
            }
        });

        bntSearch3.setText("...");
        bntSearch3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntSearch3ActionPerformed(evt);
            }
        });

        txtDictionary.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDictionaryActionPerformed(evt);
            }
        });

        labelDictionary.setText("Dicionário:");

        javax.swing.GroupLayout painelLayout = new javax.swing.GroupLayout(painel);
        painel.setLayout(painelLayout);
        painelLayout.setHorizontalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(painelLayout.createSequentialGroup()
                        .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelImageOriginal)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelLayout.createSequentialGroup()
                                .addComponent(txtImageOriginal, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(bntSearch1)
                                .addGap(61, 61, 61)))
                        .addContainerGap())
                    .addGroup(painelLayout.createSequentialGroup()
                        .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelDictionary)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, painelLayout.createSequentialGroup()
                                .addComponent(txtDictionary, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addGap(10, 10, 10)
                        .addComponent(bntSearch3)
                        .addGap(61, 61, 61))
                    .addGroup(painelLayout.createSequentialGroup()
                        .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(painelLayout.createSequentialGroup()
                                .addComponent(labelImageDestination)
                                .addGap(10, 10, 10))
                            .addGroup(painelLayout.createSequentialGroup()
                                .addComponent(txtImageDestination, javax.swing.GroupLayout.DEFAULT_SIZE, 608, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                        .addComponent(bntSearch2)
                        .addGap(61, 61, 61))
                    .addGroup(painelLayout.createSequentialGroup()
                        .addComponent(labelMethod)
                        .addContainerGap(684, Short.MAX_VALUE))
                    .addGroup(painelLayout.createSequentialGroup()
                        .addComponent(comboMethod, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(painelLayout.createSequentialGroup()
                        .addComponent(bntClean)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 443, Short.MAX_VALUE)
                        .addComponent(bntQuantifyImage, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        painelLayout.setVerticalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(labelImageOriginal)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtImageOriginal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bntSearch1, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(7, 7, 7)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(painelLayout.createSequentialGroup()
                        .addComponent(labelDictionary)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtDictionary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(painelLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(bntSearch3, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(painelLayout.createSequentialGroup()
                        .addComponent(labelImageDestination)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtImageDestination, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(painelLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addComponent(bntSearch2, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addComponent(labelMethod)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(comboMethod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bntQuantifyImage)
                    .addComponent(bntClean))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(painel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }

    private void txtImageDestinationActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void bntSearch1ActionPerformed(java.awt.event.ActionEvent evt) {
        txtImageOriginal.setText(iniFileChooser());
    }

    private void bntSearch2ActionPerformed(java.awt.event.ActionEvent evt) {
        txtImageDestination.setText(iniFileChooser());
    }

    private void bntQuantifyImageActionPerformed(java.awt.event.ActionEvent evt) {

    	if(txtDictionary.getText().equals("") || txtImageDestination.getText().equals("") || txtImageOriginal.getText().equals("")){
    		JOptionPane.showMessageDialog(this, "Todos os Campos Precisam ser Informados!","Atenção", JOptionPane.ERROR_MESSAGE);
    	}else{
	    	try{
		    	Profiler profiler = null;

		    	if(comboMethod.getSelectedItem().toString().equals("FS")){
		    		profiler = app.encodePgm(txtImageOriginal.getText(), txtDictionary.getText(), (txtImageDestination.getText().replaceAll(".pgm", "").replaceAll(".encoded", "")+".encoded"), EncodeMethod.FS);
		    	}else{
		    		profiler = app.encodePgm(txtImageOriginal.getText(), txtDictionary.getText(), (txtImageDestination.getText().replaceAll(".pgm", "").replaceAll(".encoded", "")+".encoded"), EncodeMethod.PDS);
		    	}
		    	
		    	app.decodePgm((txtImageDestination.getText().replaceAll(".pgm", "").replaceAll(".encoded", "")+".encoded"), txtDictionary.getText(), (txtImageDestination.getText().replaceAll(".pgm", "")+"_decoded.pgm"));
		    	double rate = app.codificationRate(txtDictionary.getText());
		    	double mse = app.calculateMse(txtImageOriginal.getText(), (txtImageDestination.getText().replaceAll(".pgm", "")+"_decoded.pgm"));
		    	double psnr = app.calculatePsnr(txtImageOriginal.getText(), (txtImageDestination.getText().replaceAll(".pgm", "")+"_decoded.pgm"));
		    
		    	JOptionPane.showMessageDialog(this, "Processo Finalizado!", "Fim", JOptionPane.INFORMATION_MESSAGE);
		    	
		    	ResultFrame resultFrame = new ResultFrame(profiler, rate, mse, psnr, txtImageOriginal.getText(), (txtImageDestination.getText().replaceAll(".pgm", "")+"_decoded.pgm"), txtDictionary.getText());
		        resultFrame.setVisible(true);
		        
	    	
		    }catch(Exception e){
				e.printStackTrace();
		    	JOptionPane.showMessageDialog(this, e.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
			}
	    
    	}
        
    	
    }

    private void bntCleanActionPerformed(java.awt.event.ActionEvent evt) {
        txtImageOriginal.setText(null);
        txtImageDestination.setText(null);
        txtDictionary.setText(null);
    }

    private void bntSearch3ActionPerformed(java.awt.event.ActionEvent evt) {
    	txtDictionary.setText(iniFileChooser());
    }

    private void txtDictionaryActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private String iniFileChooser(){
        jFileChooser = new JFileChooser();
        JFrame frame = new JFrame();
        jFileChooser.setVisible(true);
        jFileChooser.showOpenDialog(frame);
        String path = "";
        if(jFileChooser.getSelectedFile()!=null){
            path = jFileChooser.getSelectedFile().getAbsolutePath();
        }
        return path;
    }

   public void openInputQVFrame(){
       InputQVFrame inputFrame = new InputQVFrame();
       inputFrame.setVisible(true);
       inputFrame.setLocationRelativeTo(null);
   }

    private javax.swing.JButton bntClean;
    private javax.swing.JButton bntQuantifyImage;
    private javax.swing.JButton bntSearch1;
    private javax.swing.JButton bntSearch2;
    private javax.swing.JButton bntSearch3;
    private javax.swing.JComboBox comboMethod;
    private javax.swing.JLabel labelDictionary;
    private javax.swing.JLabel labelImageDestination;
    private javax.swing.JLabel labelImageOriginal;
    private javax.swing.JLabel labelMethod;
    private javax.swing.JPanel painel;
    private javax.swing.JTextField txtDictionary;
    private javax.swing.JTextField txtImageDestination;
    private javax.swing.JTextField txtImageOriginal;

}
